<?php namespace App\Http\Controllers\Api\Admin;

use App\Content;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SystemController extends Controller
{
    public function setContents(Request $request){
        if(Auth::guard('admin')->guest()){
            return response()->json(["level"=>"error","message"=>"不允许访问"]);
        }

        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;
        $title    = $request->get("title");
        $content  = $request->get("content");
        $desc     = $request->get("desc");
        $linkUrl  = $request->get("link_url");
        $classify = $request->get("classify");
        $imgId    = $request->get("img_id");
        /** @var Content $thatContents */
        $thatContents = Content::whereId($id)->first();

        if(!$thatContents){
            $thatContents = new Content();
        }

        if(!is_null($title))$thatContents->title = $title;
        if(!is_null($content))$thatContents->content = $content;
        if(!is_null($desc))$thatContents->desc = $desc;
        if(!is_null($linkUrl))$thatContents->link_url = $linkUrl;
        if(!is_null($classify))$thatContents->classify_id = $classify;
        if(!is_null($imgId))$thatContents->img_id = $imgId;

        $thatContents->save();

        return response()->json(["level"=>"success","message"=>"设置完成","data"=>$thatContents]);


    }

    public function delContents(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newContents = Content::whereId($id)->first();

        if(!$newContents)
            return response()->json(['level'=>'error','message' => '没有该文章！']);

        $newContents->delete();

        return response()->json(["level"=>"success","message"=>"删除完成","data"=>$newContents]);
    }
}